package com.poc.barchart;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class SimpleBarChart3 extends JPanel {
	private static final long serialVersionUID = 1L;
	private double[] value;
	private ArrayList<String> priorities= new ArrayList<String>();
	private String title;

	public SimpleBarChart3(int [] bugCount, double[] val, ArrayList<String> priority, String t) {
		priorities.add(0, "Blocker = "+bugCount[0]);
		priorities.add(1, "Critical = "+bugCount[1]);
		priorities.add(2, "Major = "+bugCount[2]);
		priorities.add(3, "Minor = "+bugCount[3]);
		priorities.add(4, "Trivial = "+bugCount[4]);
		value = val;
		title = t;
	}

	public void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
		if (value == null || value.length == 0)
			return;
		double minValue = 0;
		double maxValue = 0;
		for (int i = 0; i < value.length; i++) {
			if (minValue > value[i])
				minValue = value[i];
			if (maxValue < value[i])
				maxValue = value[i];
		}
		Dimension dim = getSize();
		int clientWidth = dim.width;
		int clientHeight = dim.height;
		int barWidth = clientWidth / value.length;
		Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
		FontMetrics titleFontMetrics = graphics.getFontMetrics(titleFont);
		Font labelFont = new Font("Book Antiqua", Font.PLAIN, 10);
		FontMetrics labelFontMetrics = graphics.getFontMetrics(labelFont);
		int titleWidth = titleFontMetrics.stringWidth(title);
		int q = titleFontMetrics.getAscent();
		int p = (clientWidth - titleWidth) / 2;
		graphics.setFont(titleFont);
		graphics.drawString(title, p, q);
		int top = titleFontMetrics.getHeight();
		int bottom = labelFontMetrics.getHeight();
		/*if (maxValue == minValue)
			return;*/
		double scale = (clientHeight - top - bottom) / (maxValue - minValue);
		q = clientHeight - labelFontMetrics.getDescent();
		graphics.setFont(labelFont);
		for (int j = 0; j < value.length; j++) {
			int valueP = j * barWidth + 1;
			int valueQ = top;
			int height = (int) (value[j] * scale);
			if (value[j] >= 0)
				valueQ += (int) ((maxValue - value[j]) * scale);
			else {
				valueQ += (int) (maxValue * scale);
				height = -height;
			}
			graphics.setColor(Color.darkGray);
			graphics.fillRect(valueP, valueQ, barWidth - 2, height);
			graphics.setColor(Color.black);
			graphics.drawRect(valueP, valueQ, barWidth - 2, height);
			int labelWidth = labelFontMetrics.stringWidth((String) priorities.get(j));
			p = j * barWidth + (barWidth - labelWidth) / 2;
			graphics.drawString((String) priorities.get(j),p, q);
		}
	}

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		JFrame frame = new JFrame();
		frame.setSize(400, 300);
		double[] value = new double[5];
		value[0] = 0.0;
		value[1] = 0.0;
		value[2] = 0.0;
		value[3] = 0.0;
		value[4] = 0.0;
		ArrayList<String> priority = new ArrayList<String>();
		String username = "root";
		String password = "password";
		String query3 = "SELECT priority.pname FROM jiraissue INNER JOIN priority ON jiraissue.priority = priority.id;";
		String url = "jdbc:mysql://localhost/jiradb";
		Class.forName("com.mysql.jdbc.Driver"); 
		Connection con = DriverManager.getConnection(url, username, password);
		
		Statement stmt = con.createStatement();
		
		ResultSet rs4 = stmt.executeQuery(query3);
		int bugCount[] = {0, 0, 0, 0, 0};
		while (rs4.next()) {
			priority.add(rs4.getString(1));
			if(rs4.getString(1).equalsIgnoreCase("Blocker")){
				bugCount[0]++;
				value[0]++;
			}
			if(rs4.getString(1).equalsIgnoreCase("Critical")){
				bugCount[1]++;
				value[1]++;
			}
			if(rs4.getString(1).equalsIgnoreCase("Major")){
				bugCount[2]++;
				value[2]++;
			}
			if(rs4.getString(1).equalsIgnoreCase("Minor")){
				bugCount[3]++;
				value[3]++;
			}
			if(rs4.getString(1).equalsIgnoreCase("Trivial")){
				bugCount[4]++;
				value[4]++;
			}
		}
		// note that getString anomalously starts bugCounting at 1, not 0
		rs4.close();		
		stmt.close();
		con.close();
		
		frame.getContentPane().add(
				new SimpleBarChart3(bugCount, value, priority, " Defect Priority"));

		WindowListener winListener = new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				System.exit(0);
			}
		};
		frame.addWindowListener(winListener);
		frame.setVisible(true);
	}
}

